﻿@using System.Globalization
@using System.Text
@using Eksponent.CropUp
@using Lu.Blog.Domain.Model.DocumentType
@using Lu.Blog.Domain.Utils
@using umbraco.MacroEngines
@using Umbraco.Web
@using Vega.USiteBuilder
@inherits Vega.USiteBuilder.UmbracoTemplatePageBase<Lu.Blog.Domain.Model.DocumentType.ContentPage>

@{
    ViewBag.BodyClass = "blog";
    Layout = "_Master.cshtml";
}

<div id="main">
    <div class="wrapper cf">
			
        <!-- posts list -->
        <div id="posts-list" class="cf">        	
            @{
                var posts = BlogContentHelper.GetAllPost(new DynamicNode(Model.Content.Id));
                foreach (var post in posts)
                {
                    string headerUrl = post.HasProperty("previewImage") ? XmlNodePickerHelper.GetImagesFromPicker(post.GetProperty("previewImage").Value).FirstOrDefault().NiceUrl : "";
                    <!-- Standard -->
                    <article class="format-standard">
                        <div class="feature-image">
                            <a href="@post.NiceUrl">
                                <img src="@CropUp.GetUrl(headerUrl, new ImageSizeArguments {CropAlias = "postView"})" alt="Alt text" />
                            </a>
                        </div>
                        <div class="box cf">
                            @{
                                DateTimeFormatInfo frDtfi = new CultureInfo("fr-FR", false).DateTimeFormat;
                            }
                            <div class="entry-date"><div class="number">@Model.Content.CreateDate.Day</div><div class="month">@Model.Content.CreateDate.ToString("MMM", frDtfi).ToUpper()</div></div>
							
                            <div class="excerpt">
                                <a href="@post.NiceUrl" class="post-heading">@post.GetProperty("contentTitle").Value</a>
                                @Html.Raw(post.GetProperty("contentBody").Value)
								
                                <p><a href="@post.NiceUrl" class="learnmore">Learn More</a></p>
                            </div>
							
                            @{
                                var authors = XmlNodePickerHelper.GetContentNodesFromPicker(post.GetProperty("author").Value);
                            }
                            <div class="meta">
                                <span class="format">@(Model.Content.GetPropertyValue<string>("label"))</span>
                                <span class="user">By @Html.Raw(ShowAuthorWithLinksHtml(authors))</span>
                                @*<span class="comments">16 comments</span>
                                <span class="tags"><a href="#">red</a>, <a href="#">cyan</a>, <a href="#">white</a>, <a href="#">blue</a></span>*@
                            </div>
								
                        </div>
                    </article>
                    <!-- ENDS  Standard -->
                }
            }
					
            @*<!-- page-navigation -->
            <div class="page-navigation cf">
                <div class="nav-next"><a href="#">← Older Entries </a></div>
                <div class="nav-previous"><a href="#">Newer Entries →</a></div>
            </div>
            <!--ENDS page-navigation -->*@
					
				
        </div>
        <!-- ENDS posts list -->
			
        <!-- sidebar -->
        <aside id="sidebar">
            <ul>
                @*<li class="block">
                    <h4>@(Model.Content.GetPropertyValue("label"))</h4>
                    @Html.Raw(Model.Content.GetPropertyValue("description"))
                </li>*@
	        		
                <li class="block">
                    <h4>@Umbraco.GetDictionaryValue("Categories")</h4>
                    <ul>
                        @{
                            IEnumerable<Category> categories = BlogContentHelper.GetAllNonEmptyCategories();
                            foreach (Category cat in categories)
                            {
                                if (cat.Id == Model.Content.Id)
                                {
                                    continue;
                                }
                                <li class="cat-item"><a href="@cat.NiceUrl" title="title">@cat.GetPropertyValue("label")<span class="post-counter"> (@cat.ChildrenAsList.Count())</span></a></li>
                            }
                        }
                    </ul>
                </li>
	        		
                <li class="block">
                    <h4>@Umbraco.GetDictionaryValue("Archives")</h4>
                    <ul>
                        @{
                            var archive = BlogContentHelper.GetArchives(new DynamicNode(Model.Content.Id));
                        }
                        @foreach (var archiveMonth in archive)
                        {
                            var monthString = archiveMonth.Date.ToString("MMMM yyyy");
                            
                            <li class="cat-item"><a class="collapsed-item" href="#">@(char.ToUpper(monthString[0]) + monthString.Substring(1)) (@archiveMonth.Posts.Count)</a>
                                <ul>
                                    @foreach (var post in archiveMonth.Posts)
                                    {
                                        var contentPage = ContentHelper.GetByNodeId<ContentPage>(post.Id);
                                        <li class="subList"><a href="@contentPage.NiceUrl">@contentPage.ContentTitle</a></li>
                                    }
                                </ul>
                            </li>
                        }
                        @*
                        <li class="cat-item"><a href="#" title="title">January</a></li>
                        <li class="cat-item"><a href="#" title="title">February</a></li>
                        <li class="cat-item"><a href="#" title="title">March</a></li>
                        *@
                    </ul>
                </li>
        		
            </ul>
        		
        </aside>
        <!-- ENDS sidebar -->
			
			
    </div><!-- ENDS WRAPPER -->
</div>

@functions
{

    private string ShowAuthorWithLinksHtml(IEnumerable<DynamicNode> authors)
    {
        var ret = new StringBuilder();
        foreach (DynamicNode author in authors)
        {
            var strb = new StringBuilder();
            strb.Append("<a href=\"");
            strb.Append(author.NiceUrl);
            strb.Append("\">");
            strb.Append(author.GetPropertyValue("username"));
            strb.Append("</a>");
            ret.Append(strb);
            ret.Append(", ");
        }
        return ret.ToString().Substring(0, ret.ToString().Count() - 2);
    }

}